package com.example.opengldemo.simpleGame.special;

import android.opengl.GLES30;

import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;


public class ParticleForDraw 
{	
	int mProgram;
    int muMVPMatrixHandle;
    int muSjFactor;
    int muBj;
    int muStartColor;
    int muEndColor;
    int maPositionHandle;
    int maTexCoorHandle;
    String mVertexShader;
    String mFragmentShader;
	
	FloatBuffer mVertexBuffer;
	FloatBuffer   mTexCoorBuffer;
    int vCount=0;   
    float halfSize;
    int texId;
    public ParticleForDraw(float halfSize,int mProgram,int texId)
    {    	
    	this.halfSize=halfSize;
    	this.mProgram=mProgram;
    	this.texId=texId;

    	initVertexData(halfSize);

    	initShader();
    }
    

    public void initVertexData(float halfSize)
    {

        vCount=6;
        float vertices[]=new float[]
        {
        	-halfSize,halfSize,0,
        	-halfSize,-halfSize,0,
        	 halfSize,halfSize,0,
        	
        	-halfSize,-halfSize,0,
        	 halfSize,-halfSize,0,
        	 halfSize,halfSize,0
        };
		


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);




        float texCoor[]=new float[]
        {
        		0,0, 0,1, 1,0,
        		0,1, 1,1, 1,0
        };        

        ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mTexCoorBuffer = cbb.asFloatBuffer();
        mTexCoorBuffer.put(texCoor);
        mTexCoorBuffer.position(0);
    }


    public void initShader()
    {

        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");

        maTexCoorHandle= GLES30.glGetAttribLocation(mProgram, "aTexCoor");

        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");  

        muSjFactor=GLES30.glGetUniformLocation(mProgram, "sjFactor");

        muBj=GLES30.glGetUniformLocation(mProgram, "bj");

        muStartColor=GLES30.glGetUniformLocation(mProgram, "startColor");

        muEndColor=GLES30.glGetUniformLocation(mProgram, "endColor");
    }
    
    public void drawSelf(float sj,float[] startColor,float[] endColor)
    {        

    	 GLES30.glUseProgram(mProgram);  

         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState3D.getFinalMatrix(), 0);

         GLES30.glUniform1f(muSjFactor, sj);

         GLES30.glUniform1f(muBj, halfSize);

         GLES30.glUniform4fv(muStartColor, 1, startColor, 0);

         GLES30.glUniform4fv(muEndColor, 1, endColor, 0);

         GLES30.glVertexAttribPointer  
         (
         		maPositionHandle,   
         		3, 
         		GLES30.GL_FLOAT, 
         		false,
                3*4,   
                mVertexBuffer
         );       

         GLES30.glVertexAttribPointer  
         (
        		maTexCoorHandle, 
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                mTexCoorBuffer
         );   

         GLES30.glEnableVertexAttribArray(maPositionHandle);  
         GLES30.glEnableVertexAttribArray(maTexCoorHandle);  
         

         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);
         

         GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount); 
    }
}
